--台前幕后
local taiqianmuhou = fk.CreateSkill {
    name = "mcdh_taiqianmuhou",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

Fk:addPoxiMethod{
    name = "taiqianmuhou",
    card_filter = function(to_select, selected, data)
        if table.contains(data[2], to_select) then return true end
        local type = Fk:getCardById(to_select).type
        return table.every(data[2], function (id)
            return Fk:getCardById(id).type ~= type
        end)
    end,
    feasible = function(selected)
        return true
    end,
}

taiqianmuhou:addEffect('active',{
    interaction = function()
        return UI.ComboBox{ choices = {"#taiqianmuhou1-choose", "#taiqianmuhou2-choose"} }
    end,
    can_use = function(self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        if self.interaction.data == "#taiqianmuhou1-choose" then
            player:drawCards(player:getHandcardNum(),self.name)
            local cards = player:getCardIds(Player.Hand)
            local get = {}
            for _, id in ipairs(cards) do
                local type = Fk:getCardById(id).type
                if table.every(get, function (id2)
                    return Fk:getCardById(id2).type ~= type
                end) then
                    table.insert(get, id)
                end
            end
            local other = table.filter(cards,function(id) return not table.contains(get,id) end)
            get = room:askForArrangeCards(player, self.name, {cards,{},"#taiqianmuhou1","#taiqianmuhou2"}, "#taiqianmuhou-choose", false, 7, {999, 3}, {0, #get}, ".", "taiqianmuhou", {other, get})[1]
            room:moveCards({
                ids = get ,
                from = player.id,
                toArea = Card.DiscardPile ,
                moveReason = fk.ReasonDiscard,
                proposer = player.id,
                skillName =  self.name,
                moveVisible = true
            })
        else
            local cards = player:getCardIds(Player.Hand)
            local get = {}
            for _, id in ipairs(cards) do
                local type = Fk:getCardById(id).type
                if table.every(get, function (id2)
                    return Fk:getCardById(id2).type ~= type
                end) then
                    table.insert(get, id)
                end
            end
            local other = table.filter(cards,function(id) return not table.contains(get,id) end)
            get = room:askForArrangeCards(player, self.name, {cards,{},"#taiqianmuhou1","#taiqianmuhou2"}, "#taiqianmuhou-choose", false, 7, {999, 3}, {0, #get}, ".", "taiqianmuhou", {other, get})[1]
            room:moveCards({
                ids = get ,
                from = player.id,
                toArea = Card.DiscardPile ,
                moveReason = fk.ReasonDiscard,
                proposer = player.id,
                skillName =  self.name,
                moveVisible = true
            })
            player:drawCards(player:getHandcardNum(),self.name)
        end
    end
})


Fk:loadTranslationTable{
    ["mcdh_taiqianmuhou"] = "台前幕后",
    [":mcdh_taiqianmuhou"] ="出牌阶段限一次，你可以执行一项，然后再执行另一项：1.将手牌数摸至翻倍；2.展示手牌并弃置至每种类别各一张。",
    ["#taiqianmuhou1-choose"] = "1.将手牌数摸至翻倍,然后展示手牌并弃置至每种类别各一张。",
    ["#taiqianmuhou2-choose"]= "2.展示手牌并弃置至每种类别各一张,然后将手牌数摸至翻倍。",
    ["#taiqianmuhou-choose"] = "台前幕后：保留不同类型的牌各一张",
    ["#taiqianmuhou1"] = "手牌",
    ["#taiqianmuhou2"] = "保留的牌",
}

return taiqianmuhou